import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../components/Home.vue'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
        redirect: '/index',
        children: [
            {
                path: '/index',
                name: 'index',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "首页"
                },
                component: () => import('../views/index.vue')
            },
            {
                path: '/teacherManage',
                name: 'teacherManage',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "教师管理"
                },
                component: () => import('../views/manage/teacherManage.vue')
            },
            {
                path: '/studentManage',
                name: 'studentManage',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "学生管理"
                },
                component: () => import('../views/manage/studentManage.vue')
            },
            {
                path: '/facultyManage',
                name: 'facultyManage',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "院系管理"
                },
                component: () => import('../views/manage/facultyManage.vue')
            },
            {
                path: '/curriculumManage',
                name: 'curriculumManage',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "课程管理"
                },
                component: () => import('../views/teacher/curriculumManage.vue')
            },
            {
                path: '/templateManage',
                name: 'templateManage',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "模板管理"
                },
                component: () => import('../views/teacher/templateManage.vue')
            },
            {
                path: '/bgMusic',
                name: 'bgMusic',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "音乐素材库"
                },
                component: () => import('../views/teacher/bgMusic.vue')
            },
            {
                path: '/jobManage',
                name: 'jobManage',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "作业管理"
                },
                component: () => import('../views/teacher/jobManage.vue')
            },
            {
                path: '/courseList',
                name: 'courseList',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "课程列表"
                },
                component: () => import('../views/student/courseList.vue')
            },


            {
                path: '/goods',
                name: 'GoodsView',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "商品列表"
                },
                component: () => import('../views/goods/GoodsView.vue')
            },
            {
                path: '/user',
                name: 'UserView',
                meta: {
                    isShow: true,  // 是否需要被展示出来
                    title: "用户列表"
                },
                component: () => import('../views/user/UserView.vue')
            },

        ]
    },

    {
        path: '/login',
        name: 'LoginView',
        component: () => import(/* webpackChunkName: "login" */ '../views/LoginView.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token: string | null = localStorage.getItem("Authorization")
    if (!token && to.path !== "/login") {
        next("/login")
    }
    else {
        next()
    }
})

export default router
